package com.shwread.android.qysw;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.ciko.code.http.Req.SignfeedbackInfos;
import com.shwread.android.qysw10000182.R;

/*
*
* 因公外出拜访反馈页面
* */

public class MyFeedBackActivity extends AppCompatActivity implements HttpResponseListener {


    private int outsideId;
    private EditText etFeedBackContent;
    private Button btnFeedBackSubmit;
    private String feedback;
    private ImageView ivHeadLefBack;
    private TextView tvHeadTitle;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_feed_back);
        outsideId = getIntent().getIntExtra("outsideId",1);
        initTopBar();
        initView();
    }
    protected void initTopBar() {
        ivHeadLefBack = (ImageView) findViewById(com.mywork.R.id.ivHeadLefBack);
        tvHeadTitle = (TextView) findViewById(com.mywork.R.id.tvHeadTitle);
        tvHeadTitle.setText("拜访反馈");
        ivHeadLefBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    protected void initView() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("加载中...");
        progressDialog.setCancelable(true);
        etFeedBackContent = (EditText) findViewById(com.mywork.R.id.etFeedBackContent);
        btnFeedBackSubmit = (Button) findViewById(com.mywork.R.id.btnFeedBackSubmit);

        btnFeedBackSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                feedback = etFeedBackContent.getText().toString().trim();
                if (TextUtils.isEmpty(feedback)){
                    Toast.makeText(MyFeedBackActivity.this,"请输入拜访反馈",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (feedback.length()>200){
                    Toast.makeText(MyFeedBackActivity.this,"字数超过200字",Toast.LENGTH_SHORT).show();
                    return;
                }
                progressDialog.show();
                doHttpRequst(outsideId,feedback);
            }
        });
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        Toast.makeText(MyFeedBackActivity.this,"拜访反馈提交成功",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("feedback",feedback);
        setResult(200,intent);
        if (progressDialog.isShowing()){
            progressDialog.dismiss();
        }
        finish();
    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        Toast.makeText(MyFeedBackActivity.this,"提交失败，请稍后重试",Toast.LENGTH_SHORT).show();
    }
    private void doHttpRequst(int outsideId, String feedback) {
        HttpClient.getInstance(this).post(new SignfeedbackInfos(outsideId,feedback), this);
    }
}

